package router

import (
	"github.com/gin-gonic/gin"
	"logi-flow/handler"
	"net/http"
)

func Init() *gin.Engine {
	// 1.创建路由
	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")
	r.POST("/ass", handler.Ass) //登录
	r.POST("/bss", handler.Bss) //// 用户和角色的关联表
	r.POST("/css", handler.Css) //// 角色和权限的关联表
	r.POST("/dss", handler.Dss) //借助用户和角色的关联信息，查询用户自身的角色
	r.POST("/ess", handler.Ess) //借助角色和权限关联信息，查询角色的权限信息
	//实现订单列表数据功能，整合订单配送员等数据信息，显示完整订单信息
	r.POST("/tss", handler.Tss)

	return r
}
func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
